//
//  WTSquarePictureDetailViewController.m
//  OneDay
//
//  Created by lanou on 16/1/16.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTSquarePictureDetailViewController.h"
#import "iCarousel.h"    // 有样式的轮播图, 例如毛玻璃效果
#import "BlurImageView.h"

@interface WTSquarePictureDetailViewController ()<iCarouselDataSource, iCarouselDelegate>

@property (nonatomic, strong) iCarousel * iCarousel;

// 毛玻璃
@property (nonatomic, strong) BlurImageView * blurV;

// 进度的图片
@property (nonatomic, strong) UILabel * currentlabel;


@end

@implementation WTSquarePictureDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor blackColor];
    
 
    
    // 毛玻璃
    _blurV = [[BlurImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _blurV.image = _imageArray[_index];
    _blurV.userInteractionEnabled = YES;
    [self.view addSubview:_blurV];
    
    
    _iCarousel = [[iCarousel alloc]initWithFrame:CGRectMake(0, (kScreenHeight - 350 * kRelativelength_Y) / 2, kScreenWidth, 350 * kRelativelength_Y)];
    _iCarousel.type = iCarouselOptionShowBackfaces;
    _iCarousel.pagingEnabled = YES;
    _iCarousel.scrollOffset = 0;
    _iCarousel.delegate = self;
    _iCarousel.dataSource = self;
    _iCarousel.userInteractionEnabled = YES;
    [self.view addSubview:_iCarousel];
    
    
    // 返回的按钮
    UIButton * backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame =  CGRectMake(15 * kRelativeLength_X, 25 * kRelativelength_Y, 30 * kRelativeLength_X, 20 * kRelativelength_Y);
    [backButton setBackgroundImage:[UIImage imageNamed:@"m-fanhui-02"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:backButton];
  
    
    
    UIView * footView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight - 40 * kRelativelength_Y, kScreenWidth, 40 * kRelativelength_Y)];
    footView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:footView];
    
    
    // 进度 ,第一张图片
    _currentlabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * kRelativeLength_X, 10 * kRelativelength_Y, 100 * kRelativeLength_X, 20 * kRelativelength_Y)];
    _currentlabel.text = [NSString stringWithFormat:@"%ld/%lu",(long)(_index + 1),(unsigned long)_imageArray.count];
    _currentlabel.backgroundColor = [UIColor whiteColor];
    [footView addSubview:_currentlabel];
    
    // 保存按钮
    UIButton * saveButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    saveButton.frame = CGRectMake(kScreenWidth - 50 * kRelativeLength_X, _currentlabel.top, 40 * kRelativeLength_X, _currentlabel.height);
    [saveButton setTitle:@"保存" forState:(UIControlStateNormal)];
    [saveButton addTarget:self action:@selector(saveButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [footView addSubview:saveButton];
    
}

// 返回的方法
- (void)backButtonClick:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

// 保存的方法
- (void)saveButtonClick:(UIButton *)button
{

    /**
     *  将图片保存到iPhone本地相册
     *  UIImage *image            图片对象
     *  id completionTarget       响应方法对象
     *  SEL completionSelector    方法
     *  void *contextInfo
     */
    NSInteger index = _iCarousel.scrollOffset / kScreenWidth;
    UIImage * image = _imageArray[index];


    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

    if (error == nil) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [alert show];

    }else{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [alert show];
    }

}

#pragma mark -- 数据源方法
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return _imageArray.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, (kScreenHeight - 350 * kRelativelength_Y) / 2, kScreenWidth, 350 * kRelativelength_Y)];
    imageV.image = _imageArray[index];
    return imageV;
}

- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel
{
    NSInteger index = (NSInteger)carousel.scrollOffset;
    _blurV.image = _imageArray[index];
    _currentlabel.text = [NSString stringWithFormat:@"%ld/%ld", index + 1, _imageArray.count];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
